package jvm;

import org.junit.Test;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhenzhong.wang
 * @Time: 2018/4/12 14:55
 */
public class JVMTest {

  /**
   * 测试堆变化
   *
   * @throws InterruptedException
   */
  @Test
  public void testOne() throws InterruptedException {
    while (true) {
      byte[] test = new byte[1024 * 1024];
      Thread.sleep(1000);
    }
  }

  /**
   * 模拟内存溢出的场景
   */
  @Test
  public void testTwo() {
    int a = 5000000;
    for (int i = 0; i < a; i++) {
      ThreadPoolExecutor thread = new ThreadPoolExecutor(20, Integer.MAX_VALUE, 30l,
          TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
      final int s = i;
      thread.execute(new Runnable() {
        @Override
        public void run() {
          System.out.println(s);
        }
      });
    }
  }

  /**
   * System类操作
   */
  @Test
  public void TestThree() {
    /*System.getenv().keySet().forEach(e -> {
      System.out.println(e + ": " + System.getenv().get(e));
    });*/
    System.out.print("123");
    System.out.println("456");
    System.out.println("11111");
    System.out.println(System.console());
  }

  /**
   * Runtime类操作
   */
  @Test
  public void testFour() {
    Runtime runtime = Runtime.getRuntime();
    // JVM的剩余内存，单位字节
    System.out.println(runtime.freeMemory());
    // JVM可用的最大内存，单位字节，默认为64m，可通过-Xmx参数调节
    System.out.println(runtime.maxMemory());

    // JMV当前已占用的总内存
    System.out.println(runtime.totalMemory());
  }
}

